{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.datasets import mnist\n",
    "import numpy as np\n",
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.pardir)\n",
    "\n",
    "(x_train, t_train), (x_test, t_test) = mnist.load_data()\n",
    "print(x_train.shape)\n",
    "print(t_train.shape)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Get random samples as a batch from trainning dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_size = x_train.shape[0]\n",
    "batch_size = 10\n",
    "batch_mask = np.random.choice(train_size, batch_size)\n",
    "\n",
    "x_batch = x_train[batch_mask]\n",
    "t_batch = t_train[batch_mask]\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- mini-batch ver. 交叉熵误差实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cross_entropy_error(y, t):\n",
    "    if y.ndim == 1:\n",
    "        t = t.reshape(1, t.size)\n",
    "        y = y.reshape(1, y.size)\n",
    "\n",
    "    batch_size = y.shape[0]\n",
    "    return -np.sum(t*np.log(y+1e-7))/batch_size\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- When `monitor data` is lable (\"2\", \"7\" and not like one-hot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cross_entropy_error(y, t):\n",
    "    if y.ndim == 1:\n",
    "        t = t.reshape(1, t.size)\n",
    "        y = y.reshape(1, y.size)\n",
    "\n",
    "    batch_size = y.shape[0]\n",
    "\n",
    "    d = y[np.arange(batch_size),t] # y[0,2] , y[1,7], y[2,0] ...\n",
    "    t = np.log(d + 1e-7)\n",
    "    return - np.sum(t)/batch_size\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 数值微分\n",
    "### 1.导数\n",
    "\n",
    "#### Numberical Differentiantion(数值微分)\n",
    "\n",
    "- bad instance   \n",
    "\n",
    "    Give a minium value to `h`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def numerical_diff(f,x):\n",
    "    h = 10e-50\n",
    "    return (f(x+h) - f(x))/h\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def numerical_diff(f, x):\n",
    "    h = 1e-4  # 0.0001\n",
    "    return (f(x+h) - f(x-h)) /(2*h)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Sample function 1:\n",
    "\n",
    "    $y=0.01_{x^2} + 0.1x$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func_1(x):\n",
    "    return 0.01*x**2 + 0.1*x\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.arange(0.0,20.0,0.1)\n",
    "y = func_1(x)\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('f(x)')\n",
    "plt.plot(x,y)\n",
    "plt.show()\n",
    "\n",
    "d1 = numerical_diff(func_1,5) # f'(5)\n",
    "d2 = numerical_diff(func_1,10) #  f'(10)\n",
    "print(d1)\n",
    "print(d2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 偏导数\n",
    "\n",
    "- Sample Funtion 2\n",
    "\n",
    "    $ f(x0,x1) = x^2_0 + x^2_1$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import  matplotlib.pyplot as plt \n",
    "from mpl_toolkits.mplot3d  import Axes3D\n",
    "\n",
    "def func_2(x):\n",
    "    return x[0]**2 +x[1]**2\n",
    "\n",
    "data = np.arange(0.0,20.0,0.1)\n",
    "\n",
    "# x = list()\n",
    "# for i in range(len(data)):\n",
    "#     x.append((data[i],data[i]))\n",
    "# x = np.array(x)\n",
    "# #print(x[0])\n",
    "\n",
    "# y = list()\n",
    "# y = func_2(x)\n",
    "# print(y[0])\n",
    "\n",
    "\n",
    "figure = plt.figure()\n",
    "ax = Axes3D(figure)\n",
    "\n",
    "X,Y = np.meshgrid(data,data)\n",
    "Z = func_2((data,data))\n",
    "print(X.shape)\n",
    "print(Y.shape)\n",
    "print(Z.shape)\n",
    "\n",
    "plt.xlabel('x1')\n",
    "plt.ylabel('x2')\n",
    "ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap= 'rainbow')\n",
    "\n",
    "#plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15 (main, Nov 20 2022, 05:40:34) \n[GCC 12.2.0]"
  },
  "vscode": {
   "interpreter": {
    "hash": "06c9051f96cf8929e99abfdbdde1a955d47fb652bf9ccb79bec5da028ad06163"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
